﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Common.XmlHandle
{
    public class XmlChinaList
    {
        private XmlDocument xdoc = new XmlDocument();

        public XmlChinaList(string path)
        {
            this.xdoc.Load(path);
        }

        public bool IsThereProvince(string province)
        {
            XmlNode root = xdoc.SelectSingleNode("China");
            XmlNodeList nodes = root.ChildNodes;
            foreach(XmlNode node in nodes)
            {
                XmlElement nod = (XmlElement)node;
                if (nod.GetAttribute("Name").Equals(province))
                {
                    return true;
                }
            }
            return false;
        }

        public bool IsThereCity(string province, string city)
        {
            XmlNode root = xdoc.SelectSingleNode("China");
            XmlNodeList nodes = root.ChildNodes;
            foreach(XmlNode node in nodes)
            {
                XmlElement nod = (XmlElement)node;
                if (nod.GetAttribute("Name").Equals(province))
                {
                    XmlNodeList nds = node.ChildNodes;
                    foreach(XmlNode nd in nds)
                    {
                        XmlElement ele = (XmlElement)nd;
                        if (ele.GetAttribute("Name").Equals(city))
                        {
                            return true;
                        }
                    }
                    return false;
                }
            }
            return false;
        }

        public bool IsThereDistrict(string province, string city, string district)
        {
            XmlNode root = xdoc.SelectSingleNode("China");
            XmlNodeList nodes = root.ChildNodes;
            foreach(XmlNode node in nodes)
            {
                XmlElement nod = (XmlElement)node;
                if (nod.GetAttribute("Name").Equals(province))
                {
                    XmlNodeList nds = node.ChildNodes;
                    foreach(XmlNode nd in nds)
                    {
                        XmlElement ele = (XmlElement)nd;
                        if (ele.GetAttribute("Name").Equals(city))
                        {
                            XmlNodeList els = ele.ChildNodes;
                            foreach(XmlNode el in els)
                            {
                                XmlElement e = (XmlElement)el;
                                if (e.GetAttribute("Name").Equals(district))
                                {
                                    return true;
                                }
                            }
                            return false;
                        }
                    }
                    return false;
                }
            }
            return false;
        }

        public string GetLocation(string province, string city, string district)
        {
            string b = null;
            XmlNode root = xdoc.SelectSingleNode("China");
            XmlNodeList nodes = root.ChildNodes;
            foreach (XmlNode node in nodes)
            {
                XmlElement nod = (XmlElement)node;
                if (nod.GetAttribute("Name").Equals(province))
                {
                    XmlNodeList nds = node.ChildNodes;
                    foreach (XmlNode nd in nds)
                    {
                        XmlElement ele = (XmlElement)nd;
                        if (ele.GetAttribute("Name").Equals(city))
                        {
                            XmlNodeList els = ele.ChildNodes;
                            foreach (XmlNode el in els)
                            {
                                XmlElement e = (XmlElement)el;
                                if (e.GetAttribute("Name").Equals(district))
                                {
                                    b += e.GetAttribute("Lon") + ",";
                                    b += e.GetAttribute("Lat");
                                    return b;
                                }
                            }
                            return b;
                        }
                    }
                    return b;
                }
            }
            return b;
        }
    }
}
